package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipSparePartsType;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipSparePartsTypeDao;
import cc.comac.mom.entity.EquipSparePartsTypeEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipSparePartsTypeDataHandler extends ServiceImpl<EquipSparePartsTypeDao, EquipSparePartsTypeEntity> {

	@Autowired
	private EquipSparePartsTypeDao equipSparePartsTypeDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean save(EquipSparePartsType obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			throw new RuntimeException("数据为空");	
		}
		EquipSparePartsTypeEntity innerData = obj.getInnerData();
		if (innerData == null) {
			throw new RuntimeException("数据为空");	
		}
		if (StringUtils.isEmpty(innerData.getCategoryNumber())) {
			throw new RuntimeException("类型编码为空");
		}
		if (StringUtils.isEmpty(innerData.getCategoryName())) {
			throw new RuntimeException("类型名称为空");
		}
		if (innerData.getEnable() < 0) {
			throw new RuntimeException("是否启用为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("category_number", innerData.getCategoryNumber());
		List<EquipSparePartsTypeEntity> selectByMap = equipSparePartsTypeDao.selectByMap(map);
		if (selectByMap != null && selectByMap.size() > 0) {
			throw new RuntimeException("类型编码:" + innerData.getCategoryNumber() + "已经存在");	
		}
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("row_status", 1);
		map1.put("category_name", innerData.getCategoryName());
		List<EquipSparePartsTypeEntity> selectByMap2 = equipSparePartsTypeDao.selectByMap(map1);
		if (selectByMap2 != null && selectByMap2.size() > 0) {
			throw new RuntimeException("类型名称:" + innerData.getCategoryName() + "已经存在");
		}
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipSparePartsTypeDao.insert(innerData);
		if (insert < 1) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;
	}

	public ResultBean importBatch(List<EquipSparePartsTypeEntity> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("导入数据为空");
		}
		for (EquipSparePartsTypeEntity item : list) {
			baseEntityDataUtil.setBaseEntityData(item);
		}
		boolean saveBatch = this.saveBatch(list);
		if (!saveBatch) {
			throw new RuntimeException("导入失败");
		}
		resultBean.setMessage("成功导入:" + list.size() + "条数据");
		return resultBean;
	}

	public ResultBean saveBatch(List<EquipSparePartsType> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("数据为空");	
		}
		ArrayList<EquipSparePartsTypeEntity> listEntity = new ArrayList<EquipSparePartsTypeEntity>();
		for (EquipSparePartsType item : list) {
			EquipSparePartsTypeEntity innerData = item.getInnerData();
			if (innerData == null) {
				throw new RuntimeException("数据为空");	
			}
			if (StringUtils.isEmpty(innerData.getCategoryNumber())) {
				throw new RuntimeException("类型编码为空");
			}
			if (StringUtils.isEmpty(innerData.getCategoryName())) {
				throw new RuntimeException("类型名称为空");
			}
			if (innerData.getEnable() < 0) {
				throw new RuntimeException("是否启用为空");
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("row_status", 1);
			map.put("category_number", innerData.getCategoryNumber());
			List<EquipSparePartsTypeEntity> selectByMap = equipSparePartsTypeDao.selectByMap(map);
			if (selectByMap != null && selectByMap.size() > 0) {
				throw new RuntimeException("类型编码:" + innerData.getCategoryNumber() + "已经存在");
			}
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("row_status", 1);
			map1.put("category_name", innerData.getCategoryName());
			List<EquipSparePartsTypeEntity> selectByMap2 = equipSparePartsTypeDao.selectByMap(map1);
			if (selectByMap2 != null && selectByMap2.size() > 0) {
				throw new RuntimeException("类型名称:" + innerData.getCategoryName() + "已经存在");
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if (!saveBatch) {
			throw new RuntimeException("新增失败");	
		}
		return resultBean;
	}

	public ResultBean update(EquipSparePartsType obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			throw new RuntimeException("数据为空");	
		}
		EquipSparePartsTypeEntity innerData = obj.getInnerData();
		if (innerData == null) {
			throw new RuntimeException("数据为空");	
		}
		if (StringUtils.isEmpty(innerData.getCategoryNumber())) {
			throw new RuntimeException("类型编码为空");
		}
		if (StringUtils.isEmpty(innerData.getCategoryName())) {
			throw new RuntimeException("类型名称为空");
		}
		if (innerData.getEnable() < 0) {
			throw new RuntimeException("是否启用为空");
		}
		EquipSparePartsTypeEntity equipSpareSartsTypeEntity = equipSparePartsTypeDao.selectById(innerData.getId());
		if (equipSpareSartsTypeEntity == null) {
			throw new RuntimeException("修改信息不存在");
		}
		if (!equipSpareSartsTypeEntity.getCategoryNumber().equals(innerData.getCategoryNumber())) {
			throw new RuntimeException("类型编码:" + equipSpareSartsTypeEntity.getCategoryNumber() + "不能修改");
		}
		if (!equipSpareSartsTypeEntity.getCategoryName().equals(innerData.getCategoryName())) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("row_status", 1);
			map.put("category_name", innerData.getCategoryName());
			List<EquipSparePartsTypeEntity> selectByMap = equipSparePartsTypeDao.selectByMap(map);
			if (selectByMap != null && selectByMap.size() > 0) {
				throw new RuntimeException("类型名称:" + innerData.getCategoryName() + "已经存在");
			}
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = equipSparePartsTypeDao.updateById(innerData);
		if (update < 1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}

	public ResultBean deleteBatch(List<EquipSparePartsType> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("数据为空");	
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for (EquipSparePartsType item : list) {
			EquipSparePartsTypeEntity innerData = item.getInnerData();
			if (innerData == null) {
				throw new RuntimeException("数据为空");	
			}
			listIds.add(innerData.getId());
		}
		List<EquipSparePartsTypeEntity> selectBatchIdsList = equipSparePartsTypeDao.selectBatchIds(listIds);
		if (selectBatchIdsList == null || selectBatchIdsList.size() < 1) {
			throw new RuntimeException("数据为空");	
		}
		for (EquipSparePartsTypeEntity item : selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}

		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		if (!updateBatchById) {
			throw new RuntimeException("删除失败");
		}

		return resultBean;
	}

	public EquipSparePartsType getById(long id) {
		EquipSparePartsTypeEntity spareSartsTypeEntity = equipSparePartsTypeDao.selectById(id);
		if (spareSartsTypeEntity != null) {
			EquipSparePartsType equipSpareSartsType = new EquipSparePartsType();
			equipSpareSartsType.setInnerData(spareSartsTypeEntity);
			return equipSpareSartsType;
		}
		return null;
	}

	public ResultDev selectByCondition(EquipSparePartsType obj) {
		QueryWrapper<EquipSparePartsTypeEntity> queryWrapper = new QueryWrapper<EquipSparePartsTypeEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getCategoryNumber() != null && !"".equals(obj.getInnerData().getCategoryNumber())) {
				queryWrapper.like("category_number", obj.getInnerData().getCategoryNumber());
			}
			if (obj.getInnerData().getCategoryName() != null && !"".equals(obj.getInnerData().getCategoryName())) {
				queryWrapper.like("category_name", obj.getInnerData().getCategoryName());
			}
		}
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		Page<EquipSparePartsTypeEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipSparePartsTypeEntity> iPage = equipSparePartsTypeDao.selectPage(page, queryWrapper);
		List<EquipSparePartsTypeEntity> list = iPage.getRecords();
		ResultDev resultDev = new ResultDev();
		resultDev.setEntityList(list);
		resultDev.setTotalQuantity((int) iPage.getTotal());
		resultDev.setPageCount((int) iPage.getPages());
		return resultDev;

	}

	public ResultDev getAllEnable() {
		ResultDev resultDev = new ResultDev();
		QueryWrapper<EquipSparePartsTypeEntity> queryWrapper = new QueryWrapper<EquipSparePartsTypeEntity>();
		queryWrapper.eq("enable", 1);
		queryWrapper.eq("row_status", 1);
		List<EquipSparePartsTypeEntity> selectList = equipSparePartsTypeDao.selectList(queryWrapper);
		resultDev.setEntityList(selectList);
		return resultDev;
	}

	public List<EquipSparePartsTypeEntity> exportSelectByCondition(EquipSparePartsType obj) {
		QueryWrapper<EquipSparePartsTypeEntity> queryWrapper = new QueryWrapper<EquipSparePartsTypeEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getCategoryNumber() != null && !"".equals(obj.getInnerData().getCategoryNumber())) {
				queryWrapper.like("category_number", obj.getInnerData().getCategoryNumber());
			}
			if (obj.getInnerData().getCategoryName() != null && !"".equals(obj.getInnerData().getCategoryName())) {
				queryWrapper.like("category_name", obj.getInnerData().getCategoryName());
			}
		}
		List<EquipSparePartsTypeEntity> list = equipSparePartsTypeDao.selectList(queryWrapper);
		return list;

	}
}
